using FluentNHibernate.Cfg;
using FluentNHibernate.Framework;
using NCommon.Data.NHibernate;
using NHibernate;
using NUnit.Framework;
using Roommate.Infrastructure.Persistence;

namespace Roommate.UnitTests.Persistence
{
    public class FixtureBase
    {
        //protected NHUnitOfWork unitOfWork;
        protected ISession session;

        [SetUp]
        public void SetupContext()
        {
            Before_each_test();
        }

        [TearDown]
        public void TearDownContext()
        {
            After_each_test();
        }

        protected virtual void Before_each_test()
        {

            var cfg = new SQLiteConfiguration()
                .InMemory()
                .ShowSql()
                .Raw("connection.release_mode", "on_close")
                .Raw("proxyfactory.factory_class",
                     "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");


            var sessionSource = new SingleConnectionSessionSourceForSQLiteInMemoryTesting(cfg.ToProperties(), new RoommatePersistenceModel());
            sessionSource.BuildSchema();
            session = sessionSource.CreateSession();

            CreateInitialData();

            session.Flush();
            session.Clear();


        }

        protected virtual void After_each_test()
        {
            session.Close();
            session.Dispose();
        }

        protected virtual void CreateInitialData()
        {
        }
    }
}